{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-25T05:05:54.202005Z",
     "start_time": "2025-06-25T05:05:54.187029Z"
    }
   },
   "source": [
    "# 深度学习基础结构学习\n",
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F"
   ],
   "outputs": [],
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "id": "d97ab16409820a46",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T05:05:54.233258Z",
     "start_time": "2025-06-25T05:05:54.218745Z"
    }
   },
   "source": [
    "net = nn.Sequential(nn.Linear(20,256),nn.ReLU(),nn.Linear(256,10))\n",
    "X = torch.randn(2,20)\n",
    "net(X)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.1436,  0.2514,  0.2954,  0.2184,  0.0528,  0.3274,  0.0017,  0.3723,\n",
       "         -0.3883,  0.6243],\n",
       "        [ 0.0973, -0.0453,  0.3790, -0.1136,  0.1266, -0.0421,  0.1509, -0.2100,\n",
       "         -0.0801,  0.2962]], grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T05:05:54.309337Z",
     "start_time": "2025-06-25T05:05:54.295641Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class MLP(nn.Module):\n",
    "\tdef __init__(self):\n",
    "\t\tsuper(MLP,self).__init__()\n",
    "\t\tself.hidden = nn.Linear(20,256)\n",
    "\t\tself.out = nn.Linear(256,10)\n",
    "\tdef forward(self,X):\n",
    "\t\treturn self.out(F.relu(self.hidden(X)))"
   ],
   "id": "8d3f5ea69843a3f5",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T05:05:54.386335Z",
     "start_time": "2025-06-25T05:05:54.372108Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net = MLP()\n",
    "net(X)"
   ],
   "id": "1f85a6a06b494da7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.5406,  0.3094, -0.2205,  0.1376,  0.6635,  0.3666, -0.1677, -0.0925,\n",
       "          0.0350,  0.0910],\n",
       "        [-0.1891,  0.0526, -0.1285,  0.0592,  0.3086, -0.2701, -0.1746, -0.2398,\n",
       "          0.0714, -0.4934]], grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T05:05:54.464590Z",
     "start_time": "2025-06-25T05:05:54.450203Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class MySequential(nn.Module):\n",
    "\tdef __init__(self,*args):\n",
    "\t\tsuper().__init__()\n",
    "\t\tfor idx, module in enumerate(args):\n",
    "\t\t\tself._modules[str(idx)] = module\n",
    "\tdef forward(self,X):\n",
    "\t\tfor block in self._modules.values():\n",
    "\t\t\tX = block(X)\n",
    "\t\treturn X"
   ],
   "id": "aa0a4f54064c763a",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T05:05:54.526672Z",
     "start_time": "2025-06-25T05:05:54.512158Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net = MySequential(nn.Linear(20,256),nn.ReLU(),nn.Linear(256,10))\n",
    "net(X)"
   ],
   "id": "74b8e5b08b1be95d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.0909, -0.2565,  0.0963,  0.3921,  0.2151,  0.1270,  0.0217, -0.3006,\n",
       "          0.0359,  0.0252],\n",
       "        [-0.1555, -0.0601, -0.1744, -0.2472, -0.0587,  0.1168, -0.2477, -0.2023,\n",
       "          0.1330,  0.2115]], grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T05:09:40.248550Z",
     "start_time": "2025-06-25T05:09:40.236822Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class FixedHiddenMLP(nn.Module):\n",
    "\tdef __init__(self):\n",
    "\t\tsuper(FixedHiddenMLP,self).__init__()\n",
    "\t\tself.rand_weight = torch.randn((20,20),requires_grad=False)\n",
    "\t\tself.linear = nn.Linear(20,20)\n",
    "\tdef forward(self,X):\n",
    "\t\tX = self.linear(X)\n",
    "\t\tX = F.relu(torch.mm(X,self.rand_weight)+1)\n",
    "\t\tX = self.linear(X)\n",
    "\t\twhile X.abs().sum() > 1:\n",
    "\t\t\tX /= 2\n",
    "\t\treturn X.sum()"
   ],
   "id": "85a7249dfcf72a8c",
   "outputs": [],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T05:09:44.566103Z",
     "start_time": "2025-06-25T05:09:44.556005Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net = FixedHiddenMLP()\n",
    "net(X)"
   ],
   "id": "5386d67c62c77e57",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(-0.1604, grad_fn=<SumBackward0>)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T05:13:18.524247Z",
     "start_time": "2025-06-25T05:13:18.502800Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class NestMLP(nn.Module):\n",
    "\tdef __init__(self):\n",
    "\t\tsuper(NestMLP,self).__init__()\n",
    "\t\tself.net = nn.Sequential(nn.Linear(20,64),nn.ReLU(),\n",
    "\t\t\t\t\t\t\t\t nn.Linear(64,32),nn.ReLU(),)\n",
    "\t\tself.linear = nn.Linear(32,16)\n",
    "\tdef forward(self, X):\n",
    "\t\treturn self.linear(self.net(X))\n",
    "\n",
    "chimera = nn.Sequential(NestMLP(),nn.Linear(16,20),FixedHiddenMLP())\n",
    "chimera(X)"
   ],
   "id": "d2b8b22c915eec81",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.0778, grad_fn=<SumBackward0>)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "81aed363398ca7c8"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.22"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
